package com.happiness.gateway.config.exception;

import cn.hutool.log.StaticLog;
import com.happiness.base.enums.ResultCodeEnum;
import com.happiness.base.http.ResultInfo;
import com.happiness.base.utils.exception.UnifiedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResultInfo handleException(Exception e){
        ResultInfo response = new ResultInfo();
        if (e instanceof UnifiedException) {
            UnifiedException unifiedException = (UnifiedException)e;
            StaticLog.error(e.getMessage());
            response.setCode(unifiedException.getCode());
            response.setMessage(e.getMessage());
        } else {
            StaticLog.error(e.getMessage(), e);
            response.setCode(ResultCodeEnum.INTERNAL_ERROR.code);
            response.setMessage(ResultCodeEnum.INTERNAL_ERROR.message);
        }
        return response;

    }

}
